package edu.sdibt;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import User2List.User2List;
import edu.DBUtil.DBConnection;
import edu.DBUtil.JdbcTemplate;
import edu.sdibt.domain.User;

public class UserTableOperationForJdbcTemplate{
	public List<User> queryAll(){
		String sql = "select *from users";
		List<User> list = JdbcTemplate.query(sql,(rs)->{
		List<User> list0 = new ArrayList<>();
		try {
			while(rs.next())
			{
				User user = new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),LocalDateTime.now());
				list0.add(user);
			}
		} catch (Exception e) {
			System.out.println("Query all method error,massage is"+e.getMessage());
		}
		
		return list0;
	},new Object[] {});
		return list;
	}
/*	public List<User> queryAll2() {
		String sql="select *from users";
		List<User> list=JdbcTemplate.query(sql, new User2List(), new Object[] {});
		return list;
	}*/
	public int addUser(User user) {
		int rowNum = 0;
		String sql="insert into users(id,name,password,sex)values(users_id.nextval,?,?,?)";
		JdbcTemplate.update(sql, user.getName(),user.getPassword(),user.getSex());
		return rowNum;
	}
	public int deleteUser(int id){
		int rowNum = 0;
		String sql = "delete from users where id=?";
		JdbcTemplate.update(sql, 1);
		return rowNum;
	}
	public  int UpdateUser(User user) {
		int rowNum = 0;
		String sql="update users set name=?,password=?,sex=? where id=?";
		JdbcTemplate.update(sql, user.getName(),user.getPassword(),user.getPassword());		
		return rowNum;
	}
}
